package com.ica.android.service;

import com.ica.model.command.AbstractCommand;
import com.ica.model.command.AcceptCallCommand;
import com.ica.model.command.ComposedCommand;
import com.ica.model.command.RejectCallCommand;
import com.ica.model.command.RingingModeCommand;
import com.ica.model.command.SilenceModeCommand;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class CustomBroadcastReceiver extends BroadcastReceiver {

	 private static final String TAG = "Phone call";
	 
	 public CustomBroadcastReceiver() {
	 }

	 @Override
	 public void onReceive(Context context, Intent intent) {
		 Log.w(TAG, "Receving....");
		 Bundle extras = intent.getExtras();
		 if (extras != null) {
			String state = extras.getString(TelephonyManager.EXTRA_STATE);
			Log.w("DEBUG", state);
			if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
				String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
				Log.w("DEBUG", phoneNumber);
				if (phoneNumber.equals("123")) { //REJECT CALL
					try {
						AbstractCommand c = new ComposedCommand();
						c.addCommand(new RejectCallCommand());
						c.addCommand(new SilenceModeCommand());
						c.execute(context);
						Toast.makeText(context, "Rejected Call", Toast.LENGTH_LONG).show();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Log.w(TAG,"error");
					}
					
				}
				else if (phoneNumber.equals("1234")) { //ACCEPT CALL
					try {
						AbstractCommand c = new ComposedCommand();
						c.addCommand(new AcceptCallCommand());
						c.addCommand(new RingingModeCommand());
						c.execute(context);
						Toast.makeText(context, "Accepted Call", Toast.LENGTH_LONG).show();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Log.w(TAG,"error");
					}
				}
			}
		 }
	 }
}
